package com.ht.demo.service.manager.monitor.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ht.demo.common.appexception.AppException;
import com.ht.demo.common.page.PageBO;
import com.ht.demo.common.pojo.bo.monitor.SysLoggerBO;
import com.ht.demo.common.pojo.qto.monitor.SysLoggerQTO;
import com.ht.demo.dao.domain.monitor.SysLoggerDO;
import com.ht.demo.service.convert.monitor.SysLoggerBeanUtil;
import com.ht.demo.service.manager.monitor.SysLoggerManager;
import com.ht.demo.service.mapper.monitor.SysLoggerMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @Author hdengxian
 * @Date 2023/5/30 16:20
 */
@Component
public class SysLoggerManagerImpl implements SysLoggerManager {
    @Resource
    private SysLoggerMapper sysLoggerMapper;

    @Override
    public Boolean insertLog(SysLoggerBO log) throws AppException {
        return sysLoggerMapper.save(SysLoggerBeanUtil.sysLoggerBOToDO(log));
    }

    @Override
    public Boolean deleteLog(Long[] ids) throws AppException {
        if (ids == null || ids.length <= 0) {
            LambdaQueryWrapper<SysLoggerDO> wrapper = new LambdaQueryWrapper<>();
            sysLoggerMapper.remove(wrapper);
        } else {
            sysLoggerMapper.removeByIds(Arrays.asList(ids));
        }
        return true;
    }

    @Override
    public PageBO<SysLoggerBO> findLoggerForPage(SysLoggerQTO qto) throws AppException {
        PageBO<SysLoggerBO> pageBO = new PageBO<>();
        if (qto == null) {
            return pageBO;
        }
        Integer count = sysLoggerMapper.findForPageCount(qto);
        if (count == null || count <= 0) {
            return pageBO;
        }
        List<SysLoggerDO> doList = sysLoggerMapper.findForPage(qto);
        pageBO.setData(SysLoggerBeanUtil.doList2BOList(doList), count);

        return pageBO;
    }
}
